﻿/**
 * @file fwave_wpcm_test2.cppw
 * Copyright (c) Gaaagaa. All rights reserved.
 * 
 * @author  : Gaaagaa
 * @date    : 2020-02-17
 * @version : 1.0.0.0
 * @brief   : 录制 wave-pcm 格式的音频文件（测试 fwave_wpcm_t）。
 */

#include "fwave_pcm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

////////////////////////////////////////////////////////////////////////////////

int main(int argc, char * argv[])
{
    x_errno_t xerr_no = X_ERR_UNKNOW;

    //======================================

    if (argc < 3)
    {
        printf("Usage: \n\t %s < src wave-pcm file path > < dst wave-pcm file path >\n", argv[0]);
        return -1;
    }

    //======================================

    fwave_rpcm_t fwave_rpcm;
    xerr_no = fwave_rpcm.open(argv[1]);
    if (XERR_FAILED(xerr_no))
    {
        printf("fwave_rpcm.open([%s]) return error code: %s\n",
               argv[1], fwave_rpcm_t::xerrno_name(xerr_no));
        return -1;
    }
 
    //======================================

    fwave_wpcm_t fwave_wpcm;
    xerr_no = fwave_wpcm.open(
                            argv[2],
                            fwave_rpcm.format().xut_samprate,
                            fwave_rpcm.format().xut_sampbits,
                            fwave_rpcm.format().xut_channels);
    if (XERR_FAILED(xerr_no))
    {
        printf("fwave_wpcm.open([%s], ...) return error code: %s\n",
               argv[2], fwave_wpcm_t::xerrno_name(xerr_no));
        return -1;
    }

    //======================================

    const x_uint32_t xut_csize = 8 * 1024;
    x_byte_t xbt_buffer[xut_csize] = { 0 };

    while (!fwave_rpcm.is_eof())
    {
        x_fsize_t xst_rbytes = fwave_rpcm.read(xbt_buffer, xut_csize);
        if (xst_rbytes > 0)
        {
            fwave_wpcm.write(xbt_buffer, xst_rbytes);
        }
    }

    //======================================

    system("pause");

    fwave_rpcm.close();
    fwave_wpcm.close();

    //======================================

    return 0;
}
